<?php

class Admin_Weixin_Controller extends Base_Controller {

  public function action_index()
  {


    return View::make('admin.weixininfo');
  }



  public function action_setinfo()
  {

    $str = DB::table('wx_public')->where('id','=',1)->first();
    if($str){
      return View::make('admin.weixinconf',array('str'=>$str));
    }else{
      return View::make('admin.weixinconf');

    }

  }

  public function action_dochange()
  {

    $type = Input::get('type');
    $name =  Input::get('name');
    $account =  Input::get('account');
    $appid =  Input::get('appid');
    $appsecret =  Input::get('appsecret');
    $token =  Input::get('token');
    $certified =  Input::get('certified');
    $addedDate = date('Y-m-d H:i:s',time());
    $str = DB::table('wx_public')->where('id','=',1)->first();
    if($str){
      $str1 = DB::table('wx_public')->where('id','=',1)->update(array('type'=>$type,'name'=>$name,'account'=>$account,'appid'=>$appid,'appsecret'=>$appsecret,'token'=>$token,'certified'=>$certified,'addedDate'=>$addedDate));
    }else{
      $str1 = DB::table('wx_public')->where('id','=',1)->insert(array('type'=>$type,'name'=>$name,'account'=>$account,'appid'=>$appid,'appsecret'=>$appsecret,'token'=>$token,'certified'=>$certified,'addedDate'=>$addedDate));
    }

    if($str1){
      return View::make('jump/jumpto',array('url'=>'index','time'=>2,'msg'=>'保存成功'));
    }else{
      return View::make('jump/jumpto',array('url'=>'index','time'=>2,'msg'=>'保存失败'));
    }

  }


  // 获取微信基本参数接口
  public function action_getdata()
  {
    $factorPgData = DB::table('wx_public')->where('id','=',1)->paginate(10);
    // var_dump($factorPgData);
    // exit;
    $res = array(
            'code'=>0,
            'msg'=>'',
            'count'=>$factorPgData->total,
            'data'=>$factorPgData->results,
            );
    return Response::json($res); //把数据转化成json返回
  }


  //获取微信菜单基本接口
  public function action_getbuttondata()
  {
    $factorPgData = DB::table('wx_button')->paginate(10);
    // var_dump($factorPgData);
    // exit;
    $res = array(
            'code'=>0,
            'msg'=>'',
            'count'=>$factorPgData->total,
            'data'=>$factorPgData->results,
            );
    return Response::json($res); //把数据转化成json返回


  }



  //显示菜单设计页面
  public function action_setbutton()
  {

    return View::make('admin/setbutton');

  }


  public function action_dele()
  {
    $id = Input::get('id');
    // $dele = -1;
    $str = DB::table('wx_button')->where('id','=',$id)->delete();
    if($str){
      return View::make('jump/successall',array('time'=>2,'msg'=>'删除成功'));
    }else{
      return View::make('jump/successall',array('time'=>2,'msg'=>'删除失败'));
    }

  }


  public function action_changebutton()
  {

    $id =Input::get('id');
    $res = DB::table('wx_button')->where('id','=',$id)->first();
    return View::make('admin.changebutton',array('res'=>$res));

  }


  //修改微信菜单
  public function action_dochangebutton()
  {
    $id = Input::get('id'); 
    $button_name = Input::get('button_name'); 
    $button_url = Input::get('button_url'); 
    $button_type = Input::get('button_type');
    $button_level = Input::get('button_level');
    if($button_level=="二级菜单"){
      $explanation = 1;
      $str = DB::table('wx_button')->where('id','=',$id)->update(array('button_name'=>$button_name,'button_type'=>$button_type,'button_url'=>$button_url,'button_level'=>$button_level,'explanation'=>$explanation));
    }else{
      $str = DB::table('wx_button')->where('id','=',$id)->update(array('button_name'=>$button_name,'button_type'=>$button_type,'button_url'=>$button_url,'button_level'=>$button_level,'explanation'=>0));
    }
    if($str){
      return View::make('jump/jumpto',array('url'=>'setbutton','time'=>2,'msg'=>'修改成功'));
    }else{
       return View::make('jump/jumpto',array('url'=>'setbutton','time'=>2,'msg'=>'修改失败'));
    }
  }

  //显示添加微信二级菜单页面
  public function action_addsubbutton()
  {
    $id = Input::get('id');
    $str = DB::table('wx_button')->where('id','=',$id)->first();
    $button_name = $str['button_name'];
    return View::make('admin/addsubbutton',array('id'=>$id,'button_name'=>$button_name));
  }



  //添加微信二级菜单
  public function action_doaddsubbutton()
  {
    $button_name = Input::get('button_name'); 
    $button_url = Input::get('button_url'); 
    $button_type = Input::get('button_type');
    $button_level = Input::get('button_level');
    $explanation = Input::get('id');
    $button_pater = Input::get('pater');
    $str1 = DB::table('wx_button')->where('explanation','=',$explanation)->first();
    if($str1==null){
      $sub_buttonid = 0;
    
    }else{
      $str2 = DB::table('wx_button')->where('explanation','=',$explanation)->order_by('sub_buttonid','desc')->first();
      $sub_buttonid = $str2['sub_buttonid']+1;

    }
    $str = DB::table('wx_button')->insert(array('button_name'=>$button_name,'button_url'=>$button_url,'button_type'=>$button_type,'button_pater'=>$button_pater,'explanation'=>$explanation,'button_level'=>$button_level,'sub_buttonid'=>$sub_buttonid));
    if($str){
      return View::make('jump/jumpto',array('url'=>'setbutton','time'=>2,'msg'=>'添加成功'));
    }else{
      return View::make('jump/jumpto',array('url'=>'setbutton','time'=>2,'msg'=>'添加失败'));
    }

  }


  //同步微信菜单
  public function action_synchro()
  { 

    $url = "http://persontest.cc/api/definetiem";
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    $output = curl_exec($ch);
    curl_close($ch);
    if($output=='ok'){
      return "同步成功";
    }else{
      return "同步失败";
    }
    
      
  }


















}